package com.jinzecloud.reportapproval.controller;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jinzecloud.reportapproval.bean.AuthenticationTask;
import com.jinzecloud.reportapproval.bean.TemperatureHumidity;
import com.jinzecloud.reportapproval.dto.AuditParam;
import com.jinzecloud.reportapproval.dto.DataStagingParam;
import com.jinzecloud.reportapproval.enums.StateEnum;
import com.jinzecloud.reportapproval.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import com.jinzecloud.reportapproval.bean.TestsSummaryTable;
import com.jinzecloud.reportapproval.service.TestsSummaryTableService;
import com.jinzecloud.reportapproval.utils.PageUtils;
import com.jinzecloud.reportapproval.utils.R;



/**
 * 生产试验汇总表
 *
 * @author æ­å°
 * @email 1971240051@qq.com
 * @date 2024-12-03 18:28:40
 */
@RestController
@RequestMapping("reportapproval/testssummarytable")
public class TestsSummaryTableController {
    @Autowired
    private TestsSummaryTableService testsSummaryTableService;

    @Autowired
    private AdminService adminService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("reportapproval:testssummarytable:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = testsSummaryTableService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("reportapproval:testssummarytable:info")
    public R info(@PathVariable("id") Integer id){
		TestsSummaryTable testsSummaryTable = testsSummaryTableService.getById(id);

        return R.ok().put("testsSummaryTable", testsSummaryTable);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("reportapproval:testssummarytable:save")
    public R save(@RequestBody TestsSummaryTable testsSummaryTable){

        List<TestsSummaryTable> projectCode = testsSummaryTableService.list(new QueryWrapper<TestsSummaryTable>().eq("project_code", testsSummaryTable.getProjectCode()));
        if (projectCode.size() > 0) return R.error("编号："+testsSummaryTable.getProjectCode()+"已存在，请勿重复添加！");

        testsSummaryTable.setStatus(StateEnum.SUBMIT.getState()).setWordPath("").setPdfPath("");
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        testsSummaryTable.setUserId(adminService.getIdByName(authentication.getName()));

        testsSummaryTableService.save(testsSummaryTable);

        return R.ok();
    }

    /**
     *
     * @param param
     * @return
     */
    @PostMapping("/auditList")
    public R auditList(@RequestBody DataStagingParam param) {
        return R.ok().setData(testsSummaryTableService.auditList(param));
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("reportapproval:testssummarytable:update")
    public R update(@RequestBody TestsSummaryTable testsSummaryTable){

        testsSummaryTableService.updateById(testsSummaryTable);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("reportapproval:testssummarytable:delete")
    public R delete(@RequestBody Integer[] ids){
		testsSummaryTableService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/viewPdf/{id}")
    public String viewPdf(@PathVariable("id") Integer id){
        return testsSummaryTableService.viewPdf(id);
    }


    @PostMapping("/review")
    public R review(@RequestBody TestsSummaryTable testsSummaryTable) {
        testsSummaryTableService.updateById(new TestsSummaryTable().setId(testsSummaryTable.getId()).setStatus(0));
        return R.ok();
    }

    @PostMapping("/audit")
    public R audit(@RequestBody AuditParam auditParam) {
        return testsSummaryTableService.audit(auditParam);
    }

    /**
     * 计算
     * @param testsSummaryTable
     * @return
     */
    private TestsSummaryTable compute(TestsSummaryTable testsSummaryTable){
        // 纯工作时间
        BigDecimal jobTime = new BigDecimal(testsSummaryTable.getJobTime());
        //班次时间
        BigDecimal sailingsTime = new BigDecimal(testsSummaryTable.getSailingsTime());
        //故障排除时间
        BigDecimal faultTime = new BigDecimal(testsSummaryTable.getFaultTime());
        //总工作量
        BigDecimal workNumber = testsSummaryTable.getWorkNumber();
        //计算有效度（%）
        BigDecimal effectiveness = calculateEffectiveness(jobTime, faultTime);
        //计算纯工作小时生产率
        BigDecimal productivity = calculateProductivity(workNumber, jobTime);

        testsSummaryTable.setEffective(effectiveness.toString());

        testsSummaryTable.setPureWorkingHour(productivity.toString());

        return testsSummaryTable;
    }

    /**
     * 计算有效度
     * @param jobTime
     * @param faultTime
     * @return
     */
    private static BigDecimal calculateEffectiveness(BigDecimal jobTime, BigDecimal faultTime) {
        // 总时间 = 纯工作时间 + 故障排除时间
        BigDecimal totalTime = jobTime.add(faultTime);

        // 有效度 = (纯工作时间 / 总时间) * 100
        BigDecimal effectiveness = jobTime.divide(totalTime, 4, RoundingMode.HALF_UP) // 保留4位小数进行计算
                .multiply(new BigDecimal("100")); // 乘以100得到百分比

        // 舍去小数点
        effectiveness = effectiveness.setScale(0, RoundingMode.DOWN);

        return effectiveness;
    }

    /**
     * 计算纯工作小时生产率
     * @param workNumber
     * @param jobTime
     * @return
     */
    private static BigDecimal calculateProductivity(BigDecimal workNumber, BigDecimal jobTime) {
        // 纯工作小时生产率 = 总工作量 / 纯工作时间
        BigDecimal productivity = workNumber.divide(jobTime, 4, RoundingMode.HALF_UP); // 先保留4位小数进行计算

        // 对结果进行四舍五入，并保留两位小数
        productivity = productivity.setScale(2, RoundingMode.HALF_UP);

        return productivity;
    }



}
